package com.zqwh.yj.sdk.utils.httpRequest;

import com.zqwh.yj.sdk.utils.LogUtils;
import com.zqwh.yj.sdk.utils.StringUtils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by eason.yang on 2017/7/10.
 */

public class RetrofitSingleton {
    private static Retrofit sRetrofit;
    private static String sUrl;
    private static boolean sShowNetworkDetail = false;
    private static final int DEFAULT_TIMEOUT = 30;
    private static String sToken;

    public RetrofitSingleton() {}

    public static synchronized Retrofit getInstance(String url){
        if(sRetrofit == null || !url.equals(sUrl)){
            HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                @Override
                public void log(String message) {
                    if (sShowNetworkDetail) {
                        LogUtils.logout(message);
                    }
                }
            });
            httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .addInterceptor(httpLoggingInterceptor)
                    .addInterceptor(new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            if (StringUtils.isEmpty(sToken)){
                                return chain.proceed(chain.request());
                            }else {
                                Request request = chain.request().newBuilder().addHeader("Authorization", "Bearer "+sToken).build();
                                return chain.proceed(request);
                            }
                        }
                    })
                    .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                    .readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                    .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                    .build();

            sRetrofit = new Retrofit.Builder()
                    .baseUrl(url)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .client(okHttpClient)
                    .build();
            sUrl = url;
        }
        return sRetrofit;
    }

    public static void clearInstance() {
        sRetrofit = null;
    }

    public static void setShowNetworkDetail(boolean showNetworkDetail) {
        RetrofitSingleton.sShowNetworkDetail = showNetworkDetail;
    }

    public static String getToken() {
        return sToken;
    }

    public static void setToken(String token) {
        RetrofitSingleton.sToken = token;
    }
}
